<?php

/**
 * 竞拍基础
 *
 * Created by PhpStorm.
 * User: evan
 * Date: 2016/11/11
 * Time: 下午10:52
 */
class Base extends MY_Cron_Controller
{
    /**
     * 是否开始竞拍
     *
     * 更新竞拍当前是否已经开始的状态
     *
     * php /data/www/shikee/web/cli.php auction/Base/is_start
     */
    public function is_start()
    {
        $now_time = now_time();
        $where = array
        (
            'start_time<' => $now_time,
            'end_time>'   => $now_time,
            'win_uid'     => 0,
            'status'      => AUCTION_STATUS_PENDING,
        );


        $this->load->model('auction/Model_auction', 'a');
        $data = $this->a->lists(0, 1000, $where);
        foreach ($data as $k => $v)
        {
            $save_rs = $this->a->save(array('status' => AUCTION_STATUS_DOING), $v['aid']);
            if (!$save_rs)
            {
                $this->log('更新竞拍为正在进行中状态失败, 竞拍ID:'.$v['aid']);
            }
        }
    }

    /**
     * 默认握手成功
     * php /data/www/shikee/web/cli.php auction/Base/default_handshake_success
     */
    public function default_handshake_success()
    {
        $this->load->model('auction/Model_auction', 'a');
        $play_time = date('Y-m-d H:i:s', strtotime("-10 minutes"));

        $where = array('status'=>AUCTION_STATUS_PENDING_HANDSHAKE,
            'handshake_status'=>AUCTION_HANDSHAKE_STATUS_NOT,
            'play_time <='=>$play_time
        );
        $data = $this->a->lists(0, 1000, $where);

        foreach ($data as $v) {

            //更新竞拍状态
            $auction_save_rs = $this->a->save(array('status' => AUCTION_STATUS_SUCCESS, 'handshake_status' => AUCTION_HANDSHAKE_STATUS_SUCCESS), $v['aid']);
            if (!$auction_save_rs) {
                continue;
            }

            //创建直播
            $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
            $create_rs = $this->ds->create($v['aid'], $v['win_uid'], $v['uid'], 0, 0, 0);
            if (200 != $create_rs['status']) {
                continue;
            }
        }
        echo 'ok';
    }
}